<?php
//
// {Language}_team{TeamID} = i.e en_team128.ics
// {Language}_group{GroupID} = i.e. en_group2630.ics
// {Language}_venue{VenueID} = i.e en_venue1685.ics
// {Language}_country{CountryID} = i.e en_country512.ics
// {Language}_overall.ics
//

include('euro2008_data.inc.php');

foreach($list as $url=>$filename){
	// get the URL
	$contents = '';

	// open file and read into variable
	@$fd = fopen($url, 'r');
	while (!feof($fd)) { $contents .= fread($fd, 8192); }
	fclose($fd);

	// save to local file
	if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
	}
   	if (fwrite($handle, $contents) === FALSE) {
       	echo "Cannot write to file ($filename)";
    	exit;
	}
	fclose($handle);

	remote_write($filename,$filename);
}

// open the file
//$fp = fopen('temp.txt', 'r');
//$filename = 'temp.txt';

//remote_write('temp.txt','temp.txt');

function remote_write($file,$remote_file,$ftp_server,$ftp_username,$ftp_password){
	
	// Connect to firewall
    $conn_id = ftp_connect($ftp_server,'21') or die("Couldn't connect to $ftp_server");

	// Open a session to an external ftp site
	$login_result = ftp_login ($conn_id, $username, $password); 
    
	// Check open
	if ((!$conn_id) || (!$login_result)) { 
		return -1; die('here'); 
	}
    
	// turn on passive mode transfers
	ftp_pasv ($conn_id, true) ;
	
	// try to upload the file
	if (!(ftp_put($conn_id, $remote_file, $file, FTP_ASCII))) {  return -1; }
    
	ftp_close($conn_id);
		
	return 0;
}

?>